package admin.iss.nus.edu.sg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import utility.iss.nus.edu.sg.StockQuote;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.PutItemRequest;
import com.amazonaws.services.dynamodb.model.PutItemResult;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.elasticmapreduce.AmazonElasticMapReduce;
import com.amazonaws.services.elasticmapreduce.model.JobFlowExecutionState;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.s3.transfer.Upload;

public class fetchData {
	private static final long serialVersionUID = 1L;
	private static StockQuote quote = null;

	static AmazonDynamoDBClient client;

	Upload upload;
	public static final String URL_SEPERATOR = "/";
	public static final String SITE_URL = "http://info.sgx.com/webprices.nsf/stkprices";
	public static final String FILE_URL_PART = "/%24File/SESprice.dat";

	private static final String TABLE_NAME = "Stock";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		fetchData();

	}

	public static void addDatatoDynamoDB(ArrayList<StockQuote> quoteList) {
		AmazonDynamoDBClient dynamo = new AmazonDynamoDBClient(getCredentials());
		dynamo.setEndpoint("https://dynamodb.ap-southeast-1.amazonaws.com");

		for (StockQuote stockQuote : quoteList) {

			Map<String, AttributeValue> item = newItem(stockQuote);
			PutItemRequest putItemRequest = new PutItemRequest(TABLE_NAME, item);
			PutItemResult putItemResult = dynamo.putItem(putItemRequest);
		}
	}

	public static Map<String, AttributeValue> newItem(StockQuote quote) {
		Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
		item.put("code", new AttributeValue(quote.getCode()));
		item.put("high", new AttributeValue().withN(Double.toString(quote
				.getHighPrice())));
		item.put("low", new AttributeValue().withN(Double.toString(quote
				.getLowPrice())));
		item.put("open", new AttributeValue().withN(Double.toString(quote
				.getOpenPrice())));
		item.put("close", new AttributeValue().withN(Double.toString(quote
				.getClosePrice())));
		item.put("volume", new AttributeValue().withN(Double.toString(quote
				.getVolume())));
		item.put("name", new AttributeValue(quote.getName()));
		item.put("date", new AttributeValue(quote.getYearMonthTradeDate()));
		return item;
	}

	public static AWSCredentials getCredentials() {
		AWSCredentials credentials = new BasicAWSCredentials(
				"AKIAI4LDMCJYHSTVLF4Q",
				"WNOyyksEIg7z4V/BPapKvRkYJ9iUz1h9MtaaRt8j");
		return credentials;
	}

	private static void fetchData() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		AmazonS3Client s3 = new AmazonS3Client(getCredentials());
		try {

			Calendar cld = Calendar.getInstance();
			
			try {
				
					Date dt = cld.getTime();
					cld.add(Calendar.DATE,1);
					System.out.println(dt);
					String fileURL = SITE_URL + URL_SEPERATOR + df.format(dt)
							+ FILE_URL_PART;
					URL url;

					url = new URL(fileURL);

					URLConnection conn;
					conn = url.openConnection();
					InputStream stream = conn.getInputStream();
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(stream));
					String quoteData = "";
					ArrayList<StockQuote> quoteList = new ArrayList<StockQuote>();
					File file = File.createTempFile("StockQuoteData", ".txt");
					Writer writer = new OutputStreamWriter(
							new FileOutputStream(file));
					while ((quoteData = reader.readLine()) != ""
							&& quoteData != null) {
						try {
							writer.write(quoteData);
							writer.write('\n');
							quote = StockQuote.parseString(quoteData);
							if (quote != null) {
								quoteList.add(quote);
							}

						} catch (Exception e) {
							e.printStackTrace();
							// TODO: handle exception
						}
					}
					file.deleteOnExit();
					stream.close();
					reader.close();

					Calendar cl = Calendar.getInstance();
					cl.add(Calendar.DATE, -20);
					String key = "stockData/" + df.format(dt);
					Date deleteDate = df.parse(df.format(cl.getTime()));

					ObjectListing ls = s3.listObjects("stockprocessing");
					List<S3ObjectSummary> lp = ls.getObjectSummaries();
					for (S3ObjectSummary p : lp) {
						String tempKey = p.getKey();
						String d = p.getKey().substring(10);
						if (d.length() == 10) {
							Date temp = df.parse(d);
							if (temp.equals(deleteDate)
									|| temp.before(deleteDate)) {
								String deletekey = "stockData/"
										+ df.format(temp);
								s3.deleteObject("stockprocessing", deletekey);
							}

						}
					}
					s3.putObject("stockprocessing", key, file);
					addDatatoDynamoDB(quoteList);
				
			} catch (FileNotFoundException e) {
				System.out.println("No file found for Today!!");
				//continue;
			}
			

		} catch (Exception e) {
			System.out.print("Exception" + e.getMessage());
			e.printStackTrace();
		}

	}

}
